//package com.ls.lserp.modules.produce.web;
//
//import com.ls.lserp.common.dynamicsource.DataSourceContextHolder;
//import com.ls.lserp.common.sfprinterCommon.SfExpressRequest;
//import com.ls.lserp.common.sfprinterCommon.SfExpressResponse;
//import com.ls.lserp.common.sfprinterCommon.XMLUtil;
//import com.ls.lserp.common.web.BaseController;
//import com.ls.lserp.dbaccess.Entity.OrderEntity;
//import com.ls.lserp.modules.produce.entity.PrinterForSfEntity;
//import com.ls.lserp.modules.produce.service.PrinterForSfService;
//import com.sf.csim.express.service.CallExpressServiceTools;
//import org.apache.shiro.authz.annotation.RequiresPermissions;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Controller;
//import org.springframework.ui.Model;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.ResponseBody;
//
//import javax.servlet.http.HttpServletResponse;
//import java.io.FileInputStream;
//import java.io.InputStream;
//import java.util.HashMap;
//import java.util.List;
//import java.util.Map;
//
//
//@Controller
//@RequestMapping(value = "${adminPath}/sfprinter/printer/")
//public class PrinterForSFController extends BaseController {
//
//	@Autowired
//    PrinterForSfService printerForSfService;
//	private String CUSTID = "";
//	private String deliverCompany = "";
//	private String deliverName = "";
//	private String deliverMobile = "";
//	private String deliverTel = "";
//	private String deliverProvince = "";
//	private String deliverCity = "";
//	private String deliverCounty = "";
//	private String deliverAddress = "";
//	private String deliverShipperCode = "";
//	private String appId = "";
//	private String appKey = "";
//	/**
//	 * 顺丰打印界面
//	 * 
//	 * 
//	 * @return
//	 */
//	@RequestMapping(value = { "" })
//	public String produceLoss(Model model) {
//		return "modules/produce/sfprinter";
//	}
//	
//	/**
//	 * 顺丰打印界面
//	 *
//	 *
//	 * @return
//	 */
//	@RequiresPermissions("sfprinter:printer:view")
//	@RequestMapping(value = { "savemailNo" })
//	@ResponseBody
//	public String saveMailNo(PrinterForSfEntity printerForSfEntity, HttpServletResponse response) throws Exception {
//		Map<Object, Object> listMap = new HashMap<Object, Object>();
//		try {
//			
//		
//		String reqXml = "";
//		DataSourceContextHolder.setDbType(DataSourceContextHolder.DATA_SOURCE_MTMSQL);
//		PrinterForSfEntity printerForSfEntity1 = printerForSfService.selectDataToXml(printerForSfEntity);
//
//		printerForSfEntity1.setBarCode(printerForSfEntity.getBarCode());
//		reqXml = selStrXml(printerForSfEntity1);
//		String returnXml = requestXml(reqXml);
//        // 下报文解析
//        SfExpressResponse responses = null;
//        List<OrderEntity> orderEntityList = null;
//		if (printerForSfEntity1.getExpressNo() == null || "".equals(printerForSfEntity1.getExpressNo())) {
//            // 下报文解析
//            responses = (SfExpressResponse)
//                    XMLUtil.convertXmlStrToObject(SfExpressResponse.class,returnXml);
//            if (responses.getHead().equals("ERR")) {
//                listMap.put("FLG","NG");
//                listMap.put("ERRORTEST",responses.getERROR().getText());
//            } else {
//                listMap.put("FLG","OK");
//                listMap.put("mailNo",responses.getBody().getOrderResponse().getMailNo());
//                // 更新合同中的快递单号字段
//                orderEntityList = printerForSfService.upDataAccounting(responses.getBody().getOrderResponse().getMailNo(),printerForSfEntity.getBarCode());
//            }
//        } else {
//        	orderEntityList = printerForSfService.upDataAccounting(printerForSfEntity1.getExpressNo(),printerForSfEntity1.getBarCode());
//            listMap.put("mailNo",printerForSfEntity1.getExpressNo());
//        }
//            listMap.put("CUSTID",CUSTID);
//            listMap.put("deliverCompany",deliverCompany);
//            listMap.put("deliverName",deliverName);
//            listMap.put("deliverMobile",deliverMobile);
//            listMap.put("deliverTel",deliverTel);
//            listMap.put("deliverProvince",deliverProvince);
//            listMap.put("deliverCity",deliverCity);
//            listMap.put("deliverCounty",deliverCounty);
//            listMap.put("deliverAddress",deliverAddress);
//            listMap.put("deliverShipperCode",deliverShipperCode);
//            listMap.put("consignerName",printerForSfEntity1.getConsignee());
//            listMap.put("consignerMobile",printerForSfEntity1.getMobile());
//            listMap.put("consignerProvince",printerForSfEntity1.getAddressProvinceCd());
//            listMap.put("consignerCity",printerForSfEntity1.getAddressCityCd());
//            listMap.put("consignerCounty",printerForSfEntity1.getAddressDistrict());
//            listMap.put("consignerAddress",printerForSfEntity1.getAddressDetail());
//            listMap.put("consignerShipperCode",printerForSfEntity1.getZipCode());
//            listMap.put("destCode",printerForSfEntity1.getCity());
//            listMap.put("cargoCount",printerForSfEntity1.getCount());
//            listMap.put("appId",appId);
//            listMap.put("appKey",appKey);
//
//
//        DataSourceContextHolder.setDbType(DataSourceContextHolder.DATA_SOURCE_ZYSQL);
//		if (orderEntityList!=null) {
//            printerForSfService.upDataOrder(orderEntityList);
//        }
//		return renderString(response, listMap);
//		} catch (Exception e) {
//			// TODO: handle exception
//			DataSourceContextHolder.setDbType(DataSourceContextHolder.DATA_SOURCE_ZYSQL);
//		}
//		return renderString(response, listMap);
//		
//	}
//	
//	/**
//	 * 顺丰打印界面
//	 *
//	 *
//	 * @return
//	 */
//	@RequiresPermissions("sfprinter:printer:view")
//	@RequestMapping(value = { "selectmailNo" })
//	@ResponseBody
//	public String selectedMailNo(PrinterForSfEntity printerForSfEntity, HttpServletResponse response) throws Exception {
//		Map<Object, Object> listMap = new HashMap<Object, Object>();
//		try {
//		String reqXml = "";
//		DataSourceContextHolder.setDbType(DataSourceContextHolder.DATA_SOURCE_MTMSQL);
//		PrinterForSfEntity printerForSfEntity1 = printerForSfService.selectDataToXml(printerForSfEntity);
//
//		printerForSfEntity1.setBarCode(printerForSfEntity.getBarCode());
//		//reqXml = selStrXml(printerForSfEntity1);
//		//String returnXml = requestXml(reqXml);
//        // 下报文解析
//        SfExpressResponse responses = null;
//        List<OrderEntity> orderEntityList = null;
//		if (printerForSfEntity.getMailNo()==null || "".equals(printerForSfEntity.getMailNo())) {
//            // 下报文解析
////            responses = (SfExpressResponse)
////                    XMLUtil.convertXmlStrToObject(SfExpressResponse.class,returnXml);
////            if (responses.getHead().equals("ERR")) {
////                listMap.put("FLG","NG");
////                listMap.put("ERRORTEST",responses.getERROR().getText());
////            } else {
//                listMap.put("FLG","OK");
//                listMap.put("mailNo","");
//                // 更新合同中的快递单号字段
//                //orderEntityList = printerForSfService.upDataAccounting(responses.getBody().getOrderResponse().getMailNo(),printerForSfEntity.getBarCode());
////            }
//        } else {
//            listMap.put("mailNo",printerForSfEntity.getMailNo());
//        }
//            listMap.put("CUSTID",CUSTID);
//            listMap.put("deliverCompany",deliverCompany);
//            listMap.put("deliverName",deliverName);
//            listMap.put("deliverMobile",deliverMobile);
//            listMap.put("deliverTel",deliverTel);
//            listMap.put("deliverProvince",deliverProvince);
//            listMap.put("deliverCity",deliverCity);
//            listMap.put("deliverCounty",deliverCounty);
//            listMap.put("deliverAddress",deliverAddress);
//            listMap.put("deliverShipperCode",deliverShipperCode);
//            listMap.put("consignerName",printerForSfEntity1.getConsignee());
//            listMap.put("consignerMobile",printerForSfEntity1.getMobile());
//            listMap.put("consignerProvince",printerForSfEntity1.getAddressProvinceCd());
//            listMap.put("consignerCity",printerForSfEntity1.getAddressCityCd());
//            listMap.put("consignerCounty",printerForSfEntity1.getAddressDistrict());
//            listMap.put("consignerAddress",printerForSfEntity1.getAddressDetail());
//            listMap.put("consignerShipperCode",printerForSfEntity1.getZipCode());
//            listMap.put("destCode",printerForSfEntity1.getCity());
//            listMap.put("cargoCount",printerForSfEntity1.getCount());
//            listMap.put("appId",appId);
//            listMap.put("appKey",appKey);
//
//
//        DataSourceContextHolder.setDbType(DataSourceContextHolder.DATA_SOURCE_ZYSQL);
//		if (orderEntityList!=null) {
//            printerForSfService.upDataOrder(orderEntityList);
//        }
//		return renderString(response, listMap);
//		} catch (Exception e) {
//			// TODO: handle exception
//			DataSourceContextHolder.setDbType(DataSourceContextHolder.DATA_SOURCE_ZYSQL);
//		}
//		return renderString(response, listMap);
//	}
//	private String selStrXml(PrinterForSfEntity printerForSfEntity){
//		StringBuilder strXML = new StringBuilder();
//		try{
//			@SuppressWarnings("resource")
//			InputStream is = new FileInputStream("C:\\收件人信息.txt");
//			byte[] bs = new byte[is.available()];
//			is.read(bs);
//			String reqXmlTemp = new String(bs,"UTF-8");
//            System.out.println("请求报文=====: " + reqXmlTemp);
//			// 下报文解析
//			SfExpressRequest request = (SfExpressRequest)
//					XMLUtil.convertXmlStrToObject(SfExpressRequest.class,reqXmlTemp);
//			strXML.append("<Request service='OrderService' lang='zh-CN'>");
//			strXML.append("<Head>" + request.getBody().getOrderResponse().getCLIENTNO() + "</Head>");
//			strXML.append("<Body><Order  ");
//			strXML.append("orderid='"+printerForSfEntity.getBusinessNo()+"' ");
//			strXML.append("express_type='1' ");
//			strXML.append("j_province='" + request.getBody().getOrderResponse().getjProvince() + "' ");
//			strXML.append("j_city='" + request.getBody().getOrderResponse().getjCity() + "' ");
//			strXML.append("j_company='" + request.getBody().getOrderResponse().getjCompany() + "' ");
//			strXML.append("j_contact='" + request.getBody().getOrderResponse().getjContacts() + "' ");
//			strXML.append("j_tel='" + request.getBody().getOrderResponse().getjMobile() + "' ");
//			strXML.append("j_address='" + request.getBody().getOrderResponse().getJaddress() + "' ");
//			strXML.append("d_province='" + printerForSfEntity.getAddressProvinceCd() + "' ");
//			strXML.append("d_city='" + printerForSfEntity.getAddressCityCd() + "' ");
//			strXML.append("d_county='" + printerForSfEntity.getAddressDistrict() + "' ");
//			strXML.append("d_company='' ");
//			strXML.append("d_contact='" + printerForSfEntity.getConsignee() + "' ");
//			strXML.append("d_tel='" + printerForSfEntity.getMobile() + "' ");
//			strXML.append("d_address='" + printerForSfEntity.getAddressDetail() + "' ");
//			strXML.append("parcel_quantity='1' ");
//			strXML.append("pay_method='1' ");
//			strXML.append("custid='" + request.getBody().getOrderResponse().getCUSTID() + "' ");
//			strXML.append("customs_batchs='' ");
//			strXML.append("cargo='衬衫' >");
//			strXML.append(" <AddedService name='COD' value='1.01' ");
//			strXML.append("value1 = '" + request.getBody().getOrderResponse().getCUSTID() + "'/></Order></Body></Request>");
//			CUSTID = request.getBody().getOrderResponse().getCUSTID();
//			deliverCompany = request.getBody().getOrderResponse().getjCompany();
//			deliverName = request.getBody().getOrderResponse().getjContacts();
//			deliverMobile = request.getBody().getOrderResponse().getjMobile();
//			deliverTel = request.getBody().getOrderResponse().getjTelphone();
//			deliverProvince =  request.getBody().getOrderResponse().getjProvince();
//			deliverCity = request.getBody().getOrderResponse().getjCity();
//			deliverCounty = request.getBody().getOrderResponse().getjCountry();
//			deliverAddress = request.getBody().getOrderResponse().getJaddress();
//			deliverShipperCode = request.getBody().getOrderResponse().getjShipperCode();
//			appId = request.getBody().getOrderResponse().getCLIENTNO();
//			appKey = request.getBody().getOrderResponse().getCHECKWORDCODE();
//		} catch (Exception e) {
//			return "";
//		}
//			return strXML.toString();
//	}
//	@SuppressWarnings("static-access")
//	private String requestXml(String xml) {
//		//丰桥平台公共测试账号
//		String reqURL = "https://bsp-oisp.sf-express.com/bsp-oisp/sfexpressService";
//		String clientCode = "DLSPZNWLJS";//此处替换为您在丰桥平台获取的顾客编码
//		String checkword = "OPLyhpXhCMdYpLWGZlaELsgXBfGKvk8F";//此处替换为您在丰桥平台获取的校验码
//		CallExpressServiceTools client = CallExpressServiceTools.getInstance();
//		System.out.println("请求报文：" + xml);
//		String respXml = client.callSfExpressServiceByCSIM(reqURL, xml, clientCode, checkword);
//
//		if (respXml != null) {
//			System.out.println("--------------------------------------");
//			System.out.println("返回报文: " + respXml);
//			System.out.println("--------------------------------------");
//		}
//		return respXml;
//	}
//}
